package com.hps.spring.cloud.initializrstart.job;

import com.hps.spring.cloud.initializrstart.service.WeatherDataCollectionService;
import com.hps.spring.cloud.initializrstart.vo.City;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: hps
 * @Description:
 * @Date: Created in 22:19 2018/8/29
 */
public class WeatherDataSyncJob extends QuartzJobBean {

    private static final Logger logger = LoggerFactory.getLogger(WeatherDataSyncJob.class);

    @Autowired
    private WeatherDataCollectionService weatherDataCollectionService;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        logger.info("Weather Data Sync Job");
        // TODO 改为由城市数据API微服务来提供数据
        List<City> cityList = new ArrayList<>();
        try {
            cityList = new ArrayList<>();
            City city = new City();
            city.setCityId("101280601");
            cityList.add(city);
        } catch (Exception e) {
            e.printStackTrace();
        }
        cityList.stream().map(City::getCityId).forEach(cityId -> {
            logger.info("Weather Data Sync Job, cityId:{}", cityId);
            weatherDataCollectionService.syncDataByCityId(cityId);
        });
    }
}
